package com.tanami.rynmar;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;

public class RynmarActivity extends Activity {
    	
	ProgressDialog _hourGlass;
	
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        
    	super.onCreate(savedInstanceState);
        
    	_hourGlass = ProgressDialog.show(RynmarActivity.this, "", 
    			getString(R.string.loading_please_wait), true);
    	
        try {        	
        	PriceListDownloadTask downloadTask = new PriceListDownloadTask(this);
        	downloadTask.execute(new URI("http://www.rynmar.pl/xml/cennik.xml"));
        }
        catch(Exception e)
        {
        	Log.e("T1Activity", "general error", e);
        	_hourGlass.dismiss();
        }
    }
        
    public void OnDownloadComplete(ByteArrayOutputStream data)
    {
    	try
    	{
	    	PriceListParsingTask parseTask = new PriceListParsingTask(this);
	    	
	    	parseTask.execute(data);
    	}
    	catch(Exception e)
    	{
    		Log.e("OnDownloadComplete", "general error", e);
    		_hourGlass.dismiss();
    	}	    	
    }
    /* 
     * OnParseComplete
     * */
    public void OnParseComplete(byte[] xmlContent, PriceListXmlContent data)
    {
    	ActionBar actionBar = getActionBar();
    	
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
    	setTitle(data.modifyDateTime);
    	
        setContentView(R.layout.main);
        
        _hourGlass.dismiss();
        
        if( !data.productLine.isEmpty() ) {    	
    		try
    		{
    			for (String key : data.productLine.keySet()) {	    			
	    			String label = key;
	    				    			
	    			ActionBar.Tab tab = actionBar.newTab();
	    			
	    			tab.setText(label);
	    			tab.setIcon(R.drawable.ic_tab_products);
	    			tab.setTabListener(new TabListener(this, label, ProductsListActivity.class));
	    				    				    				    			
	        	    actionBar.addTab(tab);
	    		}
    		}
    		catch(Exception e)
    		{
    			Log.e("Fill tab host", "general error", e);
    		}        	
    	}
    	
    }
}